﻿using UnityEditor;
using UnityEngine;

public class FormatTexture
{
    private const float spritePixelsPerUnit = 16;
    private const FilterMode filterMode = FilterMode.Point;
    private const TextureImporterCompression textureCompression = TextureImporterCompression.Uncompressed;

    [MenuItem("Assets/Mini Tools/Texture/规范Texture格式", false, 411)]
    private static void Main()
    {
        EditorUtils.ForeachTexture(ProcessTexture);

        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();

        Debug.Log("处理完成");
    }

    private static void ProcessTexture(TextureImporter importer, string path)
    {
        Debug.Log($"Process...  path={path}");

        importer.spritePixelsPerUnit = spritePixelsPerUnit;
        importer.filterMode = filterMode;
        importer.textureCompression = textureCompression;

        FormatPerPlatform(importer, BuildTarget.Android.ToString());
        FormatPerPlatform(importer, BuildTarget.WebGL.ToString());
        FormatPerPlatform(importer, BuildTarget.iOS.ToString());

        AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
    }

    private static void FormatPerPlatform(TextureImporter importer, string platform)
    {
        var format = importer.GetPlatformTextureSettings(platform);
        var alpha = importer.DoesSourceTextureHaveAlpha();

        format.overridden = true;
        format.format = TextureImporterFormat.ASTC_4x4;

        importer.SetPlatformTextureSettings(format);

    }
}
